<template>
  <div class="login-container">
    <div class="login-box">
      <h2>欢迎回来</h2>
      <p class="login-desc">请输入您的账户信息以继续</p>
      <div class="form-item">
        <label>用户名</label>
        <el-input v-model="loginForm.username" placeholder="请输入用户名" prefix-icon="User" />
      </div>
      <div class="form-item">
        <label>密码</label>
        <el-input v-model="loginForm.password" type="password" placeholder="请输入密码" prefix-icon="Lock" show-password />
      </div>
      <el-button
          type="primary"
          class="login-button"
          @click="handleLogin"
          :loading="loading"
      >
        登录
      </el-button>
      <div class="register-link">
        没有账户？ <router-link to="/register">立即注册</router-link>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import axios from 'axios'

const router = useRouter()
const loginForm = ref({
  username: '',
  password: ''
})
const loading = ref(false)

const handleLogin = async () => {
  if (!loginForm.value.username || !loginForm.value.password) {
    ElMessage.warning('请输入用户名和密码')
    return
  }

  loading.value = true
  try {
    const response = await axios.post('/api/auth/login', loginForm.value)
    localStorage.setItem('token', response.data.token)
    ElMessage.success('登录成功')
    router.push('/chat')
  } catch (error) {
    ElMessage.error('登录失败：' + (error.response?.data?.message || '未知错误'))
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
/* 容器样式 */
.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #667eea, #764ba2);
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

/* 登录盒子整体样式 */
.login-box {
  width: 400px;
  padding: 40px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 12px 24px rgba(0, 0, 0, 0.15);
  transition: all 0.3s ease-in-out;
}

.login-box:hover {
  transform: translateY(-5px);
}

/* 标题与描述 */
.login-box h2 {
  text-align: center;
  margin-bottom: 10px;
  font-size: 28px;
  color: #333;
  font-weight: 600;
}

.login-desc {
  text-align: center;
  color: #999;
  margin-bottom: 30px;
  font-size: 14px;
}

/* 表单项基础样式 */
.form-item {
  margin-bottom: 24px;
}

/* 表单项标签样式 */
.form-item label {
  display: block;
  margin-bottom: 6px;
  color: #333;
  font-size: 14px;
  font-weight: 500;
}

/* 输入框统一高度和样式 */
.el-input {
  height: 44px;
}

.el-input__inner {
  font-size: 14px;
  padding-left: 40px;
}

/* 按钮样式 */
.login-button {
  width: 100%;
  height: 44px;
  font-size: 16px;
  font-weight: 600;
  border-radius: 8px;
  transition: all 0.3s ease;
  background-color: #409EFF;
  border-color: #409EFF;
}

.login-button:hover {
  background-color: #5aacee;
}

.login-button:active {
  transform: scale(0.98);
}

/* 注册链接区域样式 */
.register-link {
  margin-top: 20px;
  text-align: center;
  font-size: 14px;
  color: #666;
}

.register-link a {
  color: #409EFF;
  text-decoration: none;
  font-weight: 500;
}

.register-link a:hover {
  text-decoration: underline;
}

/* 移动端适配 */
@media (max-width: 500px) {
  .login-box {
    width: 90%;
    padding: 24px;
  }

  .login-button {
    height: 40px;
  }

  .login-box h2 {
    font-size: 24px;
  }
}
</style>